package com.example.micro_benchmark_markdown

import androidx.benchmark.junit4.BenchmarkRule
import androidx.benchmark.junit4.measureRepeated
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import com.lly.markdown.MarkdownItem
import com.lly.markdown.MarkdownServer
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
 * Benchmark, which will execute on an Android device.
 *
 * The body of [BenchmarkRule.measureRepeated] is measured in a loop, and Studio will
 * output the result. Modify your code to see how it affects performance.
 */
@RunWith(AndroidJUnit4::class)
class MarkdownBenchmark {

    @get:Rule
    val benchmarkRule = BenchmarkRule()

    @Test
    fun markdownParse() {
        val instrumentation = InstrumentationRegistry.getInstrumentation()
        val targetContext = instrumentation.targetContext
        var info: List<MarkdownItem>? = null
        val device = UiDevice.getInstance(instrumentation)
//        device.executeShellCommand("am start -n com.example.micro_benchmark_markdown.test/androidx.benchmark.IsolationActivity")
        benchmarkRule.measureRepeated {
            val content = runWithTimingDisabled {
                println("targetContext:$targetContext")
                targetContext.resources.assets.open("README.md").reader().use { it.readText() }
            }
            info = MarkdownServer.parse(content, null)
        }
        println(info)
    }
}